package cz.kfad.ofrss.rss;

import java.util.List;

import android.content.Context;
import android.util.Log;
import cz.kfad.ofrss.common.NetworkHelper;
import cz.kfad.ofrss.db.DatabaseHelper;
import cz.kfad.ofrss.model.RssFeed;

public class ContentFetcherImp implements ContentFetcher {

	public static final String TAG = ContentFetcherImp.class.getSimpleName();
	private DatabaseHelper databaseHelper;

	public ContentFetcherImp(Context context) {
		databaseHelper = new DatabaseHelper(context);
	}

	@Override
	public void fetchContentForAll() {
		List<RssFeed> feeds = databaseHelper.findAllWithoutContent();
		Log.d(TAG, "count all without content: " + feeds.size());
		for (RssFeed rssFeed : feeds) {
			String content = NetworkHelper.executeHttpGet(rssFeed.getUrl());
			if (content != null) {
				Log.d(TAG, "feed " + rssFeed.getId() + " has content lenght: " + content.length());
				rssFeed.setContent(content);
			} else {
				Log.d(TAG, "feed " + rssFeed.getId() + " failed load content");
			}
			rssFeed.increaseContentAttempts();
		}
		databaseHelper.editRssFeeds(feeds);
	}


}
